<template>
  <div class="search-result-list_container">
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->

      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>
    <div class="search-list_content">
      <van-list
      v-model="loading"
      :finished="done"
      @load="pullUpLoadMoreFn"
      offset="50"
      >
        <Articleltem
          v-for="item in resultList"
          :key="item.art_id"
          :item="item"
          :isShowCross="false"
        />
      </van-list>
    </div>
  </div>
</template>

<script>
import { getSearchResultListAPI } from '@/api/search'
import Articleltem from '@/components/Articleltem.vue'
let page = 1
export default {
  components: { Articleltem },
  name: 'SearchResult',
  data () {
    return {
      resultList: [],
      loading: false,
      done: false

    }
  },
  // 接收定义的接口，拿到列表数据
  async created () {
    // 将封装的直接拿过来
    this.getSearchResultList()
  },
  computed: {
    txt () {
      return this.$route.params.keywords
    }
  },
  methods: {
    // 封装一下，调用时候方便
    async getSearchResultList () {
      const resp = await getSearchResultListAPI({
        keyword: this.txt, // 拿到关键词, keywords来源于router/index.js你定义的动态路由参数名
        page: page++
      })
      // 解构results
      console.log(resp)
      const { results } = resp.data
      // 判断一下如果小于10，就结束
      if (results.length < 10) {
        this.done = true
        return
      }
      this.resultList = [...this.resultList, ...results]
      this.loading = false
    },
    // 触底的时候执行 上拉加载更多
    pullUpLoadMoreFn () {
      if (this.resultList.length > 0) {
        this.getSearchResultList()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
